//点赞 将上次点赞cookie清除
if(!/newsShareId/.test(location.href)&&!$.cookie("newsShareId")){
    $("#thumbs-up").css("display","none");
    $("#qrcode").css("right","40px");
}

$.cookie("sharenewsid","");

var id;//转发替换ID 用于二次转发 当前获取还是cookie里保存的内容
var weixin="";//跳转地址里拼接微信地址 /weixin
var thisnick;//当前用户昵称
var newsId;//获取新闻信息
var istopnews=false;
var sharetitle="";//微信转发标题
var newsTypeId="";//新闻类型id


if($.cookie("newsid")){newsId= $.cookie("newsid");}

if(/newsid=/.test(location.href)){newsId=(location.href).match(/newsid=[0-9]+/)[0].split("=")[1];};

//获取新闻 及 处理新闻内容
function getnews(){
    var newsShareId="";
    if(/newsShareId/.test(location.href)){
        newsShareId="&shareId="+(location.href).match(/newsShareId=[0-9]+/)[0].split("=")[1];
    }else if($.cookie("newsShareId")){newsShareId="&shareId="+$.cookie("newsShareId");};
    $.ajax({
        type:"GET",
        url:"https://www.pulu.com/webapi/api/PuluNews/GetNewsById?id="+newsId+newsShareId,
        success:function(d){
            $("#waitplace").css("display","none");
            document.title =d.Data.title;
            if(d.Data.istopnews){istopnews=true;}
            $("title").html(d.Data.title);
            $("#contbox .originurl span i").html(d.Data.count);
            sharetitle=d.Data.title;
            //点赞判断
            if(d.Data.idList){
                for(var i=0;i< d.Data.idList.length;i++){
                    if(d.Data.idList[i]==$.cookie("MYID")){
                        $("#thumbs-up").css({"background":"url(img/afv.png)no-repeat center","background-size":"24px"});
                        break;
                    }
                }
            }
            //新浪房产 : 图片问题
            //环球 : 版权
            //中国会计视野、编织人生网页面替换
            //IT之家 : 地址错误（500彩票base64 微信页面）
            //新浪房产 : 加载不出来 进入他们的页面
            var forbiddenurl=/环球网|1905电影网|中国会计视野|编织人生网|IT之家|优姿美女/;
            if(d.Data.detailContent&&!forbiddenurl.test(d.Data.sitename)){
                $("h3").html(d.Data.title); //标题
                var newtimarr1=d.Data.ctime.split("-"),
                    newtimarr2=newtimarr1[2].split("T"),
                    newtimarr3=newtimarr2[1].split(":");
                newtim=newtimarr1[0]+"年"+newtimarr1[1]+"月"+newtimarr2[0]+"日"+"  "+newtimarr3[0]+":"+newtimarr3[1];
                $("#contbox .urlfrom a.pageUrl").html("<span>"+d.Data.sitename+"</span>  "+newtim);
                $("#contbox .urlfrom a.pageUrl").attr("href",d.Data.hosturl);
                newsTypeId=d.Data.newsTypeId;
                $("#contbox .originurl a").html("查看原文>");
                $("#contbox .originurl a").attr("href",d.Data.url);
                $("#contbox .originurl>span").css("display","inline-block");
                //去除注释 宽高 style class id script ?
                if((d.Data.sitename)=="网易"){
                    d.Data.detailContent=d.Data.detailContent.replace(/data-src=\"(.*?)\"/g,"").replace(/imageView&thumbnail=550x0/g,"");
                }
                if(d.Data.sitename=="叶子猪八卦") {
                    d.Data.detailContent = d.Data.detailContent.replace(/img data-src=/g, "img src=");
                }
                if(d.Data.sitename=="中外玩具网") {
                    d.Data.detailContent = d.Data.detailContent.replace(/src=/g, "data-src=");
                    d.Data.detailContent = d.Data.detailContent.replace(/original/g, "src");
                }
                d.Data.detailContent=d.Data.detailContent.replace(/\<\!\-\-(.*?)\-\-\>/g," ")
                    .replace(/width=\"[0-9]+\"/g," ")
                    .replace(/height=\"[0-9]+\"/g," ")
                    .replace(/style="[^"]*?"/g," ")
                    .replace(/class="[^"]*?"/g,"")
                    .replace(/id="[^"]*?"/g,"")
                    .replace(/<script[^>]*?>[^<]*?<\/script>/g,"")
                    .replace(/\>\?+\</g,"><")
                    .replace("提示：支持键盘“← →”键翻页","")
                    .replace("余下全文","")
                    .replace(/<a(.*?)>首页<\/a>/g,"")
                    .replace(/<a(.*?)>下一页<\/a>/g,"")
                    .replace(/<a(.*?)>共([0-9]+)页<\/a>/g,"")
                    .replace("r site","r?site")
                    .replace(/\<script/g,"<i style='display:none;'")
                    .replace(/\<\/script/g,"</i")
                    .replace("http://img1.cache.netease.com/cnews/css13/img/end_mobile.png","")
                    .replace("http://img1.cache.netease.com/cnews/css13/img/end_tech.png","")
                    .replace('src="http://p2.ifengimg.com/a/2016/0810/204c433878d5cf9size1_w16_h16.png"','')
                    .replace('src="http://style.org.hc360.com/css_hy/images/end_logo/hc_logo_end.jpg"','')
                    .replace(/\<pre/g,"<p")
                    .replace(/\/pre\>/g,"/p>")
                    .replace(/tr/g,"div")//印刷爬取问题
                    .replace(/td/g,"span")
                    .replace("/img/next_page.jpg","")
                    .replace('img src="http://static.265g.com/images/bg/ewm.jpg"','br')//二维码
                    .replace('img src="https://img.gmw.cn/pic/content_logo.png"','br')//logo
                    .replace(/>上一页</g,'><')
                    .replace(/>下一页</g,'><')
                    .replace('友情提示：支持键盘左右键“← →”翻页','')
                    .replace("http://img1.cache.netease.com/cnews/css13/img/end_news.png","");
                var newcontent=imgurlmodify(d.Data.sitename,d.Data.detailContent,d.Data.url);
                $(".contentdiv").html(newcontent);//内容
            }
            else{
                $("#iframebox").css("display","block");
                $("#contbox").css("display","none");
                $("#newsiframe").attr("src",d.Data.url);
            }
            $.cookie("newsid","");
            //微信转发
            wxshare();
        }
    });
};
getnews();


$(".listUrl").click(function(){
    if($.cookie("MYID")){
        $.cookie("newstypeid",newsTypeId);
        location.href="mycountry.html?typeid="+newsTypeId;
    }else{
        var fromuser=(location.href).match(/fromuser=[0-9a-z]+/)[0].split("=")[1];
        location.href="http://www.pulu.com/weixin/index.html?newsid="+newsId+"&fromuser="+fromuser;
    }
});


//判断是否在微信中打开
function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
}


//微信转发
function wxshare(){
    //来自分享
    //连接微信  转发
    var newsurl=location.href.replace(/http:\/\/www.pulu.com\//,"");
    var newsid=(location.href).match(/newsid=[0-9]+/)[0].match(/[0-9]+/)[0];
    var fromuser=(location.href).match(/fromuser=[0-9a-z]+/)[0].split("=")[1];
    var newsuser="";//拼接转发人
    if($.cookie("MYID")){
        newsuser=$.cookie("MYID");
    }else{
        newsuser=fromuser;
    }
    (function(){
        $.ajax({
            type:"GET",
            url:"GetSign.ashx?p="+newsurl,
            success:function(data){
                timestampone=data.timestamp;
                nonceStrone=data.nonceStr;
                signatureone=data.signature;
                wx.config({
                    debug:false,
                    appId: 'wx658c34793ecfd9f9',
                    timestamp:timestampone, //时间戳
                    nonceStr:nonceStrone,  //随机字符串
                    signature:signatureone,  //SHA1编码
                    jsApiList: [
                        // 所有要调用的 API 都要加到这个列表中
                        "onMenuShareTimeline","onMenuShareAppMessage"
                    ]
                });
            }
        })
    })();
    wx.ready(function(){
        if(!sharetitle){sharetitle="铺路资讯"}
        //微信转发到朋友圈
        wx.onMenuShareTimeline({  //分享标题  昵称      关键词
            title:sharetitle,
            link:"http://www.pulu.com/weixin/newsdetail.html?newsid="+newsid+"#fromuser="+newsuser, // 分享链接
            imgUrl:"https://www.pulu.com/weixin/img/puluweixin.jpg", // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
                if(/newsid=[0-9]+/.test(location.href)){
                    //增加转发次数
                    $.ajax({
                        type:"post",
                        url:addSendWXNumurl,
                        data:{"qid":newsid,"qtype":1},
                        success:function(){
                            alert("分享成功");
                        }
                    })
                }else{alert("分享成功");}
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
//微信转发给好友
        wx.onMenuShareAppMessage({
            title:sharetitle, // 分享标题
            desc: "Pulu铺路经验，好朋友互相铺路", // 分享描述
            link:"http://www.pulu.com/weixin/newsdetail.html?newsid="+newsid+"#fromuser="+newsuser, // 分享链接
            imgUrl:"https://www.pulu.com/weixin/img/puluweixin.jpg", // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                if(/newsid=[0-9]+/.test(location.href)){
                    //增加转发次数
                    $.ajax({
                        type:"post",
                        url:addSendWXNumurl,
                        data:{"qid":newsid,"qtype":1},
                        success:function(){
                            alert("分享成功");
                        }
                    })
                }else{alert("分享成功");}
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                //alert("取消了");
            }
        });
    });
}


//返回按钮
$(".scrollgoback").click(function(){history.go(-1);});


//去主页
$(".scrollhome").click(function(){
    if($.cookie('MYID')){location.href="pulu.html";}
});


//二维码 关注弹框
// if(is_weixn()&&$.cookie("MYID")){
//     $.ajax({
//         type:"get",
//         url:harassurl+$.cookie("MYID"),
//         success:function(d){
//             //console.log(d);
//             //没有关注
//             if(!d.Data){
//                 $("#harass").css("display","block");
//             }
//         }
//     });
// }


//关闭二维码弹框
$("#closeharass").click(function(){$("#harass").css("display","none");});


//我已关注按钮
$("#closeharass1").click(function(){
    if($.cookie("MYID")){
        $.ajax({
            type:"post",
            url:changeqrurl,
            data:{"userId":$.cookie("MYID")},
            success:function(d){$("#harass").css("display","none");}
        })
    }
    else{$("#harass").css("display","none");}
});